home *** CD-ROM | disk | FTP | other *** search
- Path: nntphub.cb.att.com!news
- From: Aaron Watters <arw@big.att.com>
- Newsgroups: comp.object,comp.lang.eiffel,comp.lang.c++,comp.lang.beta,comp.lang.java,comp.lang.sather,comp.lang.python
- Subject: Re: What Should An Exception Handling Do? -- Clarification of rules
- Date: Tue, 26 Mar 1996 18:10:33 -0500
- Organization: AT&T
- Message-ID: <31587969.5259@big.att.com>
- References: <Doq3sv.MzA@research.att.com> <1996Mar25.160702.14229@schbbs.mot.com> <4j948d$t3d@trumpet.uni-mannheim.de>
- NNTP-Posting-Host: lone.info.att.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (X11; I; SunOS 5.4 sun4m)
- CC: arw@big.att.com
-
- Someone way up in this thread wanted to know what errors
- were good for other than aborting...
-
- Some uses for exceptions, other than as aborts, in python:
-
- # get or create a data item from an archive
- try:
- person = people[name]
- except KeyError:
- person = people[name] = Person(name)
-
- # ignore broken urls, but follow the good ones (stolen from a spyder)
- urllistpending = [starturl]
- goodurls = []
- while urllistpending:
- thisurl = urllistpending[-1] # get/delete last pending
- del urllistpending[-1]
- try:
- text = thisurl.get_text() # try to follow the url
- except IOError:
- continue # url broken...
- else:
- goodurls.append(thisurl) # good url, add embedded refs
- newurls = text.urls_mentioned()
- urllistpending = urllistpending + newurls
-
- # Do an endless loop, use error to return terminating value
- # from anywhere, even deeply embedded calls (stolen from pickle.py)
- try:
- while 1:
- ........ blah blah blah .....
- except STOP, value # note: STOP is unique to this module
- result = value
-
- # is a path a directory? (even if it doesn't exist?) (from dospath.py)
- def isdir(path):
- try:
- st = os.stat(path)
- except os.error:
- return 0
- else:
- return stat.S_ISDIR(st[stat.ST_MODE])
-
- # print the object using the .print method, if there is one or use default printing
- # otherwise
- try:
- print object.print()
- except AttributeError:
- print object
-
- # and the best of all, if you have a socket module, use it, else use
- # the Mac module SOCKS, or the WinSock module, if available
- try:
- import socket
- except ImportError:
- try:
- import SOCKS
- socket = SOCKS
- except ImportError:
- import WinSock
- socket = WinSock
-
- -- Aaron Watters
- ====
- "If falling in love is anything like learning how to spell, I don't
- want to do it. It takes too long."
- Glenn, age 7 (mailed all over the world, original attribution lost).
-